package health.lm.com.shop.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.jakewharton.rxbinding2.view.RxView;
import com.lm.sjy.titlebar.widget.CommonTitleBar;
import com.vondear.rxtool.RxSPTool;
import com.zzhoujay.richtext.RichText;

import java.util.List;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import health.lm.com.component_base.base.mvp.activity.XActivity;
import health.lm.com.component_base.helper.ImageLoaderHelper;
import health.lm.com.data.AppModel;
import health.lm.com.router.ARouterConstant;
import health.lm.com.shop.R;
import health.lm.com.shop.R2;
import health.lm.com.shop.adapter.HLMyCompanyLeaderAdapter;
import health.lm.com.shop.adapter.HLPeopleAdapter;
import health.lm.com.shop.adapter.OrganizationAdapter;
import health.lm.com.shop.entity.HLMyCompanyEntity;
import health.lm.com.shop.entity.HLPeopleEntity;
import health.lm.com.shop.presenter.HLMyCompanyPresenter;

/**
 * Created by Android Studio .
 * Project：hlian health.lm.com.shop.activity
 * Date：2019/8/12 16:23
 * Introduce：我的商会
 */
@Route(path = ARouterConstant.HLMyCompanyActivity)
public class HLMyCompanyActivity extends XActivity<HLMyCompanyPresenter> {
    @Autowired
    String id;
    @Autowired
    String title;
    @BindView(R2.id.title_bar)
    CommonTitleBar mTitleBar;
    @BindView(R2.id.imageView9)
    ImageView mImageView9;
    @BindView(R2.id.textView36)
    TextView mTextView36;
    @BindView(R2.id.tv_member_num)
    TextView mTvMemberNum;
    @BindView(R2.id.ll_member_num)
    LinearLayout mLlMemberNum;
    @BindView(R2.id.tv_add_num)
    TextView mTvAddNum;
    @BindView(R2.id.ll_add_num)
    LinearLayout mLlAddNum;
    @BindView(R2.id.tv_followed)
    TextView mTvFollowed;
    @BindView(R2.id.ll_followed)
    LinearLayout mLlFollowed;
    @BindView(R2.id.linearLayout4)
    LinearLayout mLinearLayout4;
    @BindView(R2.id.textView37)
    TextView mTextView37;
    @BindView(R2.id.tabLayout)
    TabLayout mTabLayout;
    @BindView(R2.id.viewPager)
    ViewPager mViewPager;
    @BindView(R2.id.textView38)
    TextView mTextView38;
    @BindView(R2.id.textView39)
    TextView mTextView39;
    @BindView(R2.id.textView40)
    TextView mTextView40;
    @BindView(R2.id.textView41)
    TextView mTextView41;
    @BindView(R2.id.rv_list)
    RecyclerView mRvList;
    @BindView(R2.id.rv_leader_list)
    RecyclerView mRvLeaderList;
    @BindView(R2.id.tv_introduce)
    TextView mTvIntroduce;
    @BindView(R2.id.tv_title)
    TextView mTvTitle;
    @BindView(R2.id.linearLayout5)
    LinearLayout mLinearLayout5;
    @BindView(R2.id.rv_list_organization)
    RecyclerView mRvListOrganization;
    @BindView(R2.id.ll_organization)
    LinearLayout mLlOrganization;

    @Override
    public int getLayoutId() {
        return R.layout.hl_activity_my_company;
    }

    @Override
    public HLMyCompanyPresenter newP() {
        return new HLMyCompanyPresenter();
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        mTitleBar.setListener((v, action, extra) -> finish());
        mTitleBar.getCenterTextView().setText(title);

        if (TextUtils.isEmpty(id)) id = RxSPTool.getString(context, AppModel.my_business_id);
        getP().getData(id);
    }

    @SuppressLint({"CheckResult", "SetTextI18n"})
    public void setNewData(HLMyCompanyEntity pNewData) {
        mRvLeaderList.setVisibility(View.VISIBLE);
        mLlOrganization.setVisibility(View.GONE);
        mTvIntroduce.setVisibility(View.GONE);

        ImageLoaderHelper.getInstance().load(context, pNewData.getCompany_img(), mImageView9);
        mTextView36.setText(pNewData.getCompany_name());
        mTvMemberNum.setText(pNewData.getMember_num());
        mTvAddNum.setText(pNewData.getMember_add_num());
        mTvFollowed.setText(pNewData.getMember_followed());
        //组织架构名称
        mTvTitle.setText(pNewData.getCompany_name() + "组织架构");
        //领导列表 三纵数列
        initLeaderData(pNewData.getEntityList());

        HLPeopleAdapter peopleAdapter = new HLPeopleAdapter(pNewData.getPeopleEntities());
        mRvList.setNestedScrollingEnabled(false);
        mRvList.setLayoutManager(new LinearLayoutManager(context));
        mRvList.setAdapter(peopleAdapter);
        peopleAdapter.setOnItemClickListener((adapter, view, position) -> {
            HLPeopleEntity entity = (HLPeopleEntity) adapter.getData().get(position);
            ARouter.getInstance().build(ARouterConstant.HLPeopleDetailActivity)
                    .withString("id", entity.getId() + "")
                    .withString("name", entity.getName())
                    .navigation();
        });

        //TODO 组织结构图 地址(具体样式待定)
        OrganizationAdapter organizationAdapter = new OrganizationAdapter(pNewData.getOrganization_list());
        mRvListOrganization.setLayoutManager(new LinearLayoutManager(context));
        organizationAdapter.openLoadAnimation();
        mRvListOrganization.setAdapter(organizationAdapter);
        //ImageLoaderHelper.getInstance().load(context, "http://image109.360doc.com/DownloadImg/2018/03/1008/126782666_1_2018031008485635.jpeg", mIvOriange);
        RichText.initCacheDir(context);
        RichText.fromHtml(pNewData.getIntroduction()).into(mTvIntroduce);
        //List<Fragment> fragments = new ArrayList<>();
        //HLMyCompanyFragment companyFragment1 = new HLMyCompanyFragment();
        //HLMyCompanyFragment companyFragment2 = new HLMyCompanyFragment();
        //HLMyCompanyFragment companyFragment3 = new HLMyCompanyFragment();
        //Bundle bundle = new Bundle();
        //bundle.putInt("type", 1);
        //bundle.putParcelableArrayList("data", (ArrayList<? extends Parcelable>) pNewData.getEntityList());
        //companyFragment1.setArguments(bundle);
//
        //Bundle bundle2 = new Bundle();
        //bundle2.putInt("type", 2);
        //bundle2.putString("data", "http://image109.360doc.com/DownloadImg/2018/03/1008/126782666_1_2018031008485635.jpeg");
        //companyFragment2.setArguments(bundle2);
//
        //Bundle bundle3 = new Bundle();
        //bundle3.putInt("type", 3);
        //bundle3.putString("data", pNewData.getIntroduction());
        //companyFragment3.setArguments(bundle3);
        //fragments.add(companyFragment1);
        //fragments.add(companyFragment2);
        //fragments.add(companyFragment3);
//
        //List<String> title = new ArrayList<>();
        //title.add("商会领导人");
        //title.add("组织架构");
        //title.add("商会简介");
//
        //mViewPager.setAdapter(new BaseFragmentStateAdapter(getSupportFragmentManager(), fragments, title));
        //mTabLayout.setupWithViewPager(mViewPager);
        mTabLayout.addTab(mTabLayout.newTab().setText("商会领导人"));
        mTabLayout.addTab(mTabLayout.newTab().setText("组织架构"));
        mTabLayout.addTab(mTabLayout.newTab().setText("商会简介"));
        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab pTab) {
                switch (pTab.getPosition()) {
                    case 0:
                        mRvLeaderList.setVisibility(View.VISIBLE);
                        mLlOrganization.setVisibility(View.GONE);
                        mTvIntroduce.setVisibility(View.GONE);
                        mTextView38.setVisibility(View.VISIBLE);
                        break;
                    case 1:
                        mRvLeaderList.setVisibility(View.GONE);
                        mLlOrganization.setVisibility(View.VISIBLE);
                        mTvIntroduce.setVisibility(View.GONE);
                        mTextView38.setVisibility(View.GONE);
                        break;
                    case 2:
                        mRvLeaderList.setVisibility(View.GONE);
                        mLlOrganization.setVisibility(View.GONE);
                        mTextView38.setVisibility(View.GONE);
                        mTvIntroduce.setVisibility(View.VISIBLE);
                        break;
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab pTab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab pTab) {

            }
        });

        //商会领导 更多领导
        RxView.clicks(mTextView38).throttleFirst(1, TimeUnit.SECONDS).subscribe(v -> {
            ARouter.getInstance().build(ARouterConstant.HLCompanyLeaderActivity)
                    .withString("id", id)
                    .navigation();
        });
        //商会会员 查看更多
        RxView.clicks(mTextView41).throttleFirst(1, TimeUnit.SECONDS).subscribe(v -> {
            ARouter.getInstance().build(ARouterConstant.CompanyMemberListActivity)
                    .withString("id", id)
                    .navigation();
        });
    }

    private void initLeaderData(List<HLMyCompanyEntity.LeaderEntity> pList) {
        HLMyCompanyLeaderAdapter leaderAdapter = new HLMyCompanyLeaderAdapter(pList);
        mRvLeaderList.setLayoutManager(new GridLayoutManager(context, 3));
        mRvLeaderList.setNestedScrollingEnabled(false);
        mRvLeaderList.setAdapter(leaderAdapter);
        leaderAdapter.setOnItemClickListener((adapter, view, position) -> {
            HLMyCompanyEntity.LeaderEntity entity = (HLMyCompanyEntity.LeaderEntity) adapter.getData().get(position);
            ARouter.getInstance().build(ARouterConstant.HLPeopleDetailActivity)
                    .withString("id", entity.getId())
                    .withString("name", entity.getName())
                    .navigation();
        });
    }
}
